﻿//-----------------------------------------------------------------------
// <copyright file="TelemetryObjectContainer.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>This class is the container of telemetry data, come from telemetry function test</summary>
// <author>v-haiwan</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Microsoft.Reputation.Services;
using Microsoft.Reputation.Tests.FrontEndCommon;

using System.ServiceModel.Channels;

namespace Microsoft.Reputation.Tests.ServiceabilityTest.Helper
{
    /// <summary>
    /// Container of Telemetry data
    /// </summary>
    public class TelemetryObjectContainer
    {
        /// <summary>
        /// samplingRate field
        /// </summary>
        private uint m_samplingRate;

        /// <summary>
        /// communicationDelayMinutes field
        /// </summary>
        private uint m_communicationDelayMinutes;

        /// <summary>
        /// clientIdToken field
        /// </summary>
        private ClientIdToken m_clientIdToken;

        /// <summary>
        /// send request result
        /// </summary>
        private uint m_result;

        /// <summary>
        /// telemClient object
        /// </summary>
        private MrsTelemetryServiceClient m_telemClient;

        /// <summary>
        /// urlTelemetry data property
        /// </summary>
        public UrlTelemetry UrlTelemetry 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// urlErrorReports property
        /// </summary>
        public UrlErrorReport[] UrlErrorReports 
        { 
            get; 
            set; 
        }
        
        /// <summary>
        /// send request Result property
        /// </summary>
        public uint Result
        {
            get
            {
                return m_result;
            }
        }

        /// <summary>
        /// SamplingRate property
        /// </summary>
        public uint SamplingRate
        {
            get
            {
                return m_samplingRate;
            }
        }

        /// <summary>
        /// CommunicationDelayMinutes property
        /// </summary>
        public uint CommunicationDelayMinutes
        {
            get
            {
                return m_communicationDelayMinutes;
            }
        }

        /// <summary>
        /// Initializes a new instance of the TelemetryObjectContainer class
        /// </summary>
        public TelemetryObjectContainer()
        {
        }

        /// <summary>
        /// Initializes a new instance of the TelemetryObjectContainer class
        /// </summary>
        /// <param name="serviceURL">telemetry web service URL</param>
        public TelemetryObjectContainer(string serviceURL)
        {
            m_telemClient = new MrsTelemetryServiceClient(CreateBinding(serviceURL), new EndpointAddress(serviceURL));
        }

        /// <summary>
        /// Initialize UrlTelemetry data
        /// </summary>
        /// <param name="createObject">create Object</param>
        public void InitializeUrlTelemetry(bool createObject)
        {
            if (createObject)
            {
                UrlTelemetry = new UrlTelemetry();

                UrlTelemetry.BlockOverruleList = null;
                UrlTelemetry.CategoryCountList = null;
                UrlTelemetry.CategoryPrecedenceCountList = null;
                UrlTelemetry.OverrideExceptionList = null;
                UrlTelemetry.SampleUrls = null;
                UrlTelemetry.UrlCounts = null;
            }
            else
            {
                UrlTelemetry = null;
            }

            m_clientIdToken = null;
        }

        /// <summary>
        /// Initialize UrlErrorReport
        /// </summary>
        /// <param name="createObject">create Object</param>
        /// <param name="size">UrlErrorReport size</param>
        public void InitializeUrlErrorReport(bool createObject, int size)
        {
            if (createObject)
            {
                UrlErrorReports = new UrlErrorReport[size];
            }
            else
            {
                UrlErrorReports = null;
            }

            m_clientIdToken = null;
        }

        /// <summary>
        /// Make StartTime
        /// </summary>
        /// <param name="startTimeType">startTime Type</param>
        public void MakeStartTime(string startTimeType)
        {
            UrlTelemetry.StartTime = TelemetryHelper.GetDateTime(startTimeType);
        }

        /// <summary>
        /// Make EndTime
        /// </summary>
        /// <param name="endTimeType">endTime Type</param>
        public void MakeEndTime(string endTimeType)
        {
            UrlTelemetry.EndTime = TelemetryHelper.GetDateTime(endTimeType);
        }

        /// <summary>
        /// Make ClientIdToken Object
        /// </summary>
        public void MakeClientIdTokenObject()
        {
            m_clientIdToken = new ClientIdToken();
            m_clientIdToken.ClientId = new Guid("4228ac1a-2e5c-40f1-b193-aa5f078c8e83");      // DO NOT CHANGE - This is the test GUID being used and BackEnd checks for this GUID to separate test data.
            m_clientIdToken.CustomerId = new Guid("3fca792f-bbb7-4d38-83e4-1993a994f6a4");    // DO NOT CHANGE - This is the test GUID being used and BackEnd checks for this GUID to separate test data.
            m_clientIdToken.CustomerVersion = Environment.OSVersion.VersionString;
            m_clientIdToken.DeploymentId = new Guid("59867f97-e895-4397-b1e7-5d7c1326da48");  // DO NOT CHANGE - This is the test GUID being used and BackEnd checks for this GUID to separate test data.
        }

        /// <summary>
        /// Call SendUrlTelemetry
        /// </summary>
        /// <returns>SendUrlTelemetry result</returns>
        public uint MakeSendUrlTelemetryCall()
        {
            m_result = m_telemClient.SendUrlUsageReport(out m_samplingRate, out m_communicationDelayMinutes, m_clientIdToken, UrlTelemetry);
            return m_result;
        }
        
        /// <summary>
        /// Call SendUrlErrorReports
        /// </summary>      
        /// <returns>SendUrlErrorReports result</returns>
        public uint MakeSendUrlErrorReportsCall()
        {            
            m_result = m_telemClient.SendUrlErrorReports(out m_samplingRate, out m_communicationDelayMinutes, m_clientIdToken, UrlErrorReports);
            return m_result;
        }

        /// <summary>
        /// Helper method to create telemetry service connection binding.
        /// </summary>
        /// <param name="serviceURL">telemetry web service URL</param>
        /// <returns>binding object</returns>
        private Binding CreateBinding(string serviceURL)
        {
            BasicHttpBinding binding = new BasicHttpBinding();

            binding.MaxReceivedMessageSize = 16777216;
            binding.SendTimeout = new TimeSpan(2, 0, 0);
            binding.ReceiveTimeout = new TimeSpan(2, 0, 0);

            if (serviceURL.StartsWith("https:"))
            {
                binding.Security.Mode = BasicHttpSecurityMode.Transport;
            }
            else
            {
                binding.Security.Mode = BasicHttpSecurityMode.None;
            }

            return binding;
        }
    }
}
